package com.taormina.searchservice;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public abstract class HttpPostSearchService extends AbstractSearchService {

	@Override
	protected InputStream getWebSearch(String query1, String query2, String query3) throws ClientProtocolException, IOException {

		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(getSearchUrl());
		
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append(query1 + " " + query2 + " " + query3);
		
		List<NameValuePair> parameters = new ArrayList<NameValuePair>();		
		parameters.add(new BasicNameValuePair(getQueryPrefix(), stringBuilder.toString()));
		post.setEntity(new UrlEncodedFormEntity(parameters));
		
		HttpResponse response = client.execute(post);
		return response.getEntity().getContent();
	}
}
